import 'package:flutter/material.dart';



 class TextFieldDemo extends StatefulWidget{

@override
_TextFieldDemo createState() => _TextFieldDemo();
 
  
}

class _TextFieldDemo extends State<TextFieldDemo>{

@override
  Widget build(BuildContext context) {
    GlobalKey _formKey = GlobalKey<FormState>();
    TextEditingController nameController = TextEditingController();
    TextEditingController _pwsController = TextEditingController();
    TextEditingController unameController = TextEditingController(text: "helloworld");
    unameController.selection = TextSelection(baseOffset: 2, extentOffset: unameController.text.length);
    unameController.addListener(() {
      print(unameController.text);
    });
    print(unameController.text);
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text('TextField'),),
      body: Center(
       child: Column(children: [
         TextField(autofocus: true,decoration: InputDecoration(labelText: '用户名',hintText: '用户名或者邮箱',prefixIcon: Icon(Icons.person)),),
         TextField(decoration: InputDecoration(prefixIcon: Icon(Icons.lock),labelText: '密码',hintText: '登陆密码'),obscureText: true,controller: unameController,),
       Text('以下是Form监控'),
       Form(
         key: _formKey,
         autovalidateMode: AutovalidateMode.onUserInteraction,
         child: Column(children: [
           TextFormField(
             autofocus: true,
             controller: nameController,
             decoration: InputDecoration(hintText: '用户名或邮箱',labelText: '用户名',icon: Icon(Icons.person),),
             validator: (v){
               return v!.trim().length > 0 ? null:'用户名不能为空';
             },
             
           ),
           TextFormField(
             controller: _pwsController,
             decoration: InputDecoration(labelText: '密码',hintText: '登陆密码',icon: Icon(Icons.lock)),
             obscureText: true,
             validator: (v){
               return v!.trim().length > 5 ?null:'密码不能少于6位';
             },
           ),
           Padding(padding: const EdgeInsets.only(top: 28.0),
           child: Row(
             children: [
               Expanded(child: ElevatedButton(onPressed: (){
  if ((_formKey.currentState as FormState).validate()){
    
  }
               }, child: Padding(padding: const EdgeInsets.all(16),child: Text("登陆"),))),
               
             ],
           ),
           )

       ],),)
       ],),
      ),
      
    );

  }
}

